package si.unimb.src.wifisms.ui;

import java.util.ArrayList;

import si.unimb.src.wifisms.R;
import si.unimb.src.wifisms.entity.SMSMessage;
import si.unimb.src.wifisms.util.Utility;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SMSMessageArrayAdapter extends ArrayAdapter<SMSMessage> {

	private ArrayList<SMSMessage> contentAll;
	private Context context;
	private int resourceId;
	
	public SMSMessageArrayAdapter(Context context, int textViewResourceId, ArrayList<SMSMessage> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.contentAll = objects;
		this.resourceId = textViewResourceId;
		
		System.out.println("contect: " + context + " contentAll: " + contentAll + " resourceId: " + resourceId);
	}

	/**
	 * Funkcija poveže podatke z View-i
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View v = convertView;

		/* nastavimo pravi layout */
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(this.resourceId, null);
		}
		
		System.out.println("view: " + v);

		SMSMessage msg  = contentAll.get(position);
		if (msg != null) {
			
			System.out.println("msg: " + msg.getStatus());
			
			/* dobimo TextView, ki ga želimo in v njega zapišemo podatke */
			TextView recipients = (TextView)v.findViewById(R.id.RecipientsTextView);
			TextView message = (TextView)v.findViewById(R.id.MessageTextView);
			TextView status = (TextView)v.findViewById(R.id.MessageStatusTextView);
		
			recipients.setText(Utility.ArrayListToString(msg.getRecipients()));
			message.setText(msg.getMessage());
			status.setText(msg.getStatusString());
			
		}
		
		return v;
	}
}
